#ifndef _LCTHREAD_H
#define _LCTHREAD_H
#include <QObject>
#include <QThread>
#include "rtdb.h"
#include "ComStruct.h"
#include "KgGlobalData.h"
#include <QSettings>
#include <QDir>

#define KG_MAX_TIME_NUMBER  96    
#define MAX_AUTONOMY_NUMBER 12

struct TimeSegment {
    int nStartMinute; // 起始分钟
    int nEndMinute;   // 结束分钟
	TimeSegment() : nStartMinute(0), nEndMinute(0) {}
    TimeSegment(int startMinute, int endMinute) :
        nStartMinute(startMinute),
        nEndMinute(endMinute) {}
};

enum
{
    PCS_STOP=1,
    PCS_STANDBY,
    PCS_RESET,
    PCS_START,
    PCS_CHARGE,
    PCS_DISCHARGE,
};

class StrategyThread: public QThread
{
    Q_OBJECT
public:
	StrategyThread(QObject *parent = 0, CRTDB *pRTDB = 0);
	~StrategyThread();
private:
	void run();	

	CRTDB *m_pRTDB;
	CBattStack *m_pBattStack;

    void funcPcs1(void);
    void initializeRangStruct(void);
    void getLocalCurveYgPower(void);
    void getLocalCurveWgPower(void);
    void getRemoteCurveYgPower(void); 
    void getRemoteCurveWgPower(void); 
    void getXLNL(void);	
	float XL(float &fYgLast);
	float NLFunc0(float &fYgLast);	
	float NLFunc1(float &fYgLast);	
	float m_fYgPower;
	float m_fWgPower;
	float m_fNLYgLast;
	float m_fXLYgLast;
    float m_NLPoint;
	int m_NLMode;
    int m_NLMeter;
    int m_XLMeter;
	QDateTime XLUpDelay;
	QDateTime XLDownDelay;	
	QDateTime NLUpDelay;
	QDateTime NLDownDelay[2];
    float m_fXLYgPower;
    float m_fNLYgPower[2];
    float m_XLToday;
    float m_XLmonth[12];
    float m_XLmargin;
    float m_NLmargin;
    bool m_bNL;
    bool m_bXL;
    TimeSegment m_timeSegment[KG_MAX_TIME_NUMBER];    
    float m_LocalCurveYgPower[KG_MAX_TIME_NUMBER];
    float m_LocalCurveWgPower[KG_MAX_TIME_NUMBER];
    float m_RemoteCurveYgPower[KG_MAX_TIME_NUMBER];
    float m_RemoteCurveWgPower[KG_MAX_TIME_NUMBER];
    /*
    策略1
    m_fRev[99] 最大温度
    m_fRev[98] 最小温度    
    m_fRev[97] 平均温度    
    m_fRev[96] 综合温度
    m_fRev[95] 开关机
    策略2
    m_fRev[99] 最大温度
    m_fRev[98] 最小温度    
    m_fRev[97] 平均温度    
    m_fRev[96] 综合温度
    m_fRev[95] 读取空调湿度
    m_uRev[98] 最大温度所处状态
    m_uRev[97] 最小温度所处状态
    m_uRev[96] 湿度所处状态
    m_uRev[95] 下发开关机 0关机 1开机
    */
    void funcAc1(CAcData *pAcData, int index);
    void funcAc2(CAcData *pAcData, int index);
    /*
    m_fRev[99] 最大温度
    m_fRev[98] 最小温度    
    m_fRev[97] 平均温度    
    m_fRev[96] 综合温度
    */
    void funcDh1(CDhData *pDhData, int index);    
    /*
    m_uRev[98] 读取液冷故障状态
    m_fRev[99] 读取回水温度
    
    m_uRev[97] 下发工作模式 0待机 11一级制冷 12二级制冷 2制热 3循环
    m_fRev[98] 下发目标温度

    m_uRev[96] 充放电状态 0待机 1充电 2放电 (±1A)
    m_fRev[97] 最大温度
    m_fRev[96] 最小温度    
    m_fRev[95] 平均温度
    m_fRev[94] 综合温度     
    */
    void funcLc1(CLcData *pLcData, int index);
    void getTmsStateOnStandby(CLcData *pLcData);
    void getTmsStateOnCool(CLcData *pLcData);
    void getTmsStateOnHeat(CLcData *pLcData);
    void getTmsStateOnLoop(CLcData *pLcData);    
    float m_fSglTempColdDownValLev1;
    float m_fSglTempColdUpValLev1;
    float m_fAvgTempColdDownValLev1;
    float m_fSglTempColdDownValLev2;
    float m_fAvgTempColdDownValLev2;
    float m_fSglTempStopColdUpVal;
    float m_fAvgTempStopColdUpVal;
    float m_fSglTempHeatUpVal;
    float m_fAvgTempHeatUpVal;
    float m_fSglTempStopHeatDownVal;
    float m_fAvgTempStopHeatDownVal;
    float m_fSglTempLoopDownVal;
    float m_fSglTempLoopUpVal;
    float m_fAvgTempLoopDownVal;
    float m_fAvgTempLoopUpVal;
    float m_fTmsHuiShuiTempLoopUpVal;
    float m_fSglTempDiffLoopDownVal;
    float m_fSglTempDiffLoopUpVal;
    float m_fSglTempStopLoopUpVal;
    float m_fSglTempStopLoopDownVal;	
    float m_fCoolTempLev1;
    float m_fCoolTempLev2;
    float m_fHeatTemp;
    float m_fStopTemp;
    float m_fMaxTemp;
    float m_fMinTemp;
    float m_fAvgTemp;    
    float m_fDifTemp;    

    float tempf;
    quint16 temp16;

    int m_isOutputLog;    
 	KgLogFile m_logFile; 
    QString m_strLog;      
    void devConfig(void); 
    void writeKgLogfile(QString strInfo,bool bSaveToFile=false);  
};

#define KG_MAX_LOG_SIZE     1024*1024*10
#define KG_LOG_INFO(MESSAGE)\
	do{ \
        QString strInfor;\
        QString currentFileName = QFileInfo(__FILE__).fileName();\
        QString currentFileLine = QString::number(__LINE__);\
        strInfor = currentFileName + "(" + currentFileLine + ") " + MESSAGE;\
        writeKgLogfile(strInfor,true);\
	} while(0)

#endif
